package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.sql.DataSource;

import oracle.jdbc.proxy.annotation.Pre;

import org.springframework.stereotype.Component;

import com.oocl.dao.CustomerDao;
import com.oocl.pojo.Customer;

@Component(value="daoImpl")
public class CustomerDaoImpl implements CustomerDao{
	
	public CustomerDaoImpl() {
		System.out.println("dao实例化");
	}
	
	@Resource(name="dataSource")
	private DataSource ds;

	public void setDataSource(DataSource ds) {
		this.ds = ds;
		System.out.println(ds);
	}

	@Override
	public Customer add(Customer customer) {
		String sql = "insert into  customer(id,name,sex,sal,birth) values(?,?,?,?,?)";
		Connection con=null;
		PreparedStatement pst =null;
		try {
			con = ds.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, customer.getName());
			pst.setBoolean(3, customer.isSex());
			pst.setDouble(4, customer.getSal());
			pst.setDate(5, new java.sql.Date(customer.getBirth().getTime()));
			pst.executeUpdate();
			con.close();
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return customer;
	}

	@Override
	public String delete(String id) {
		String sql = "delete customer where id=?";
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con=ds.getConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, id);
			pst.executeUpdate();
			con.close();
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "{}";
	}

	@Override
	public Customer update(Customer customer) {
		String sql = "update customer set name=?,sex=?,sal=?,birth=? where id=?";
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = ds.getConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, customer.getName());
			pst.setBoolean(2, customer.isSex());
			pst.setDouble(3, customer.getSal());
			pst.setDate(4, new java.sql.Date(customer.getBirth().getTime()));
			pst.setString(5, customer.getId());
			pst.executeUpdate();
			con.close();
			pst.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return customer;
	}

	@Override
	public List<Customer> findAll() {
		String sql = "select * from customer";
		List<Customer> customers = new ArrayList<Customer>();
		Connection con= null;
		PreparedStatement pst = null;
		ResultSet rs= null;
		try {
			con =ds.getConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				Customer c = new Customer(rs.getString(1), rs.getString(2), rs.getBoolean(3), rs.getDouble(4), rs.getDate(5));
				customers.add(c);
			}
			con.close();
			pst.close();
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customers;
	}

}
